<template>
  <view>
    <!-- search bar -->
    <view class="search-container">
      <uni-search-bar focus='true' cancelButton="none" radius="50" @input="inputHandler"></uni-search-bar>
    </view>
    
    <!-- 搜索历史 -->
    <view class="search-history" v-if="searchList.length === 0">
      <view class="search-history-title">
        <view>搜索历史</view>
        <uni-icons type="trash" @click="clearHistory"></uni-icons>
      </view>
      
      <view class="search-history-list" >
        <!-- <view class=""> -->
          <uni-tag class="search-history-list-item" :text="item" v-for="(item, i) in searchHistory" :key="i" @click="gotoGoodsList(item)"></uni-tag>
        <!-- </view> -->
      </view>
    </view>
    
    <!-- 搜索结果列表 -->
    <view class="search-list" v-else>
      <view class="search-item" v-for="(item, i) in searchList" :key="i" @click="gotoGoodsDetail(item)">
        <view class="search-item-info">
          {{item.goods_name}}
        </view>
        <uni-icons type="arrowright"></uni-icons>
      </view>
    </view>
    
  </view>
</template>

<script>
  export default {
    data() {
      return {
        kw: '', // 搜索关键词
        timerId: null,
        searchList: [], // 搜索数据列表
        searchHistory: [], // 搜索历史
      };
    },
    onLoad() {
      this.searchHistory = JSON.parse( uni.getStorageSync('kw') || '[]' )
    },
    methods: {
      inputHandler(e){
        // 防抖
        clearTimeout(this.timerId)
        this.timerId = setTimeout(() => {
          console.log('搜索输入值',e)
          this.kw = e.trim()
          this.getSearchList()
        }, 500)
      },
      
      // 搜索关键词产品
      async getSearchList(){
        
        if(!this.kw){
          this.searchList = []
          return
        } 
        
        const {data: res} = await uni.$http.get('/goods/qsearch', {query: this.kw})
        console.log(res)
        if(res.meta.status !== 200) return uni.$showMsg()
        
        // 赋值到data
        this.searchList = res.message
        
        // 去重
        const index = this.searchHistory.findIndex((item)=>{
          return item === this.kw
        })
        console.log('====', index)
        if(index>=0){ // 找到了
          this.searchHistory.splice(index, 1)
          this.searchHistory = [ this.kw ,...this.searchHistory]
        }else{
          this.searchHistory = [ this.kw ,...this.searchHistory]
        }
        
        // 保存到本地
        uni.setStorageSync('kw', JSON.stringify(this.searchHistory))
        
      },
      clearHistory(){
        this.searchHistory = []
        uni.removeStorageSync('kw')
      },
      gotoGoodsDetail(item){
        console.log(item)
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id='+item.goods_id
        })
      },
      gotoGoodsList(item){
        console.log(item)
        uni.navigateTo({
          url: '/subpkg/goods_list/goods_list?query='+item
        })
      }
    }
  }
</script>

<style lang="scss">
  .search-container {
    background-color: #c00000;
    position: sticky;
    top: 0;
    // width: 100%;
    // position: fixed;
    // top: 0;
    // z-index: 999;
  }
  
  .search-list {
    // padding-top: 50px;
    .search-item{
      display: flex;
      height: 90rpx;
      border-bottom: 1rpx solid #cccccc;
      align-items: center;
      margin: 0 16rpx;
      justify-content: space-between;
      .search-item-info{
        // 文字不允许换行（单行文本）
        white-space: nowrap;
        // 溢出部分隐藏
        overflow: hidden;
        // 文本溢出后，使用 ... 代替
        text-overflow: ellipsis;
        font-size: 26rpx;
      }

    }
  }
  
  .search-history{
    .search-history-title{
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 20rpx;
      height: 80rpx;
      border-bottom: 1rpx solid #cccccc;
      margin-bottom: 20rpx;
    }
    .search-history-list{
      .search-history-list-item{
        margin-left: 20rpx;
        margin-bottom: 20rpx;
      }
    }
  }
</style>
